package com.liyujie.dyeingmachine.ui.home.operate;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ConvertUtils;
import com.liyujie.dyeingmachine.R;
import com.liyujie.dyeingmachine.databinding.ActivityCylinderBlockBinding;
import com.liyujie.dyeingmachine.listener.OnGeneralCallback;
import com.liyujie.dyeingmachine.listener.UniversalCallback;
import com.liyujie.dyeingmachine.mobile.dao.CylinderBlockEntityDao;
import com.liyujie.dyeingmachine.mobile.entity.CylinderBlockEntity;
import com.liyujie.dyeingmachine.pop.CylinderBlockOperationPopup;
import com.liyujie.dyeingmachine.ui.BaseActivity;
import com.liyujie.dyeingmachine.ui.home.operate.adapter.CylinderBlockAdapter;
import com.liyujie.dyeingmachine.utils.InitializeUtils;
import com.liyujie.dyeingmachine.views.rc.GridItemDecoration;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupAnimation;

/**
 * 缸体管理
 */
public class CylinderBlockActivity extends BaseActivity<ActivityCylinderBlockBinding> implements UniversalCallback {


    /**
     * VM
     */
    private CylinderBlockViewModel cylinderBlockViewModel;
    /**
     * 适配器
     */
    private CylinderBlockAdapter mCylinderBlockAdapter;
    /**
     * 实体
     */
    private CylinderBlockEntity mCylinderBlockEntity;
    /**
     * 弹框
     */
    private CylinderBlockOperationPopup mCylinderBlockOperationPopup;


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_cylinder_block;

    }


    public void initViewModel() {
        //VM
        cylinderBlockViewModel = new ViewModelProvider(getViewModelStore(), new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(CylinderBlockViewModel.class);
        binding.setViewModel(cylinderBlockViewModel);

    }


    @Override
    public void initView() {
        super.initView();
        binding.rv.setLayoutManager(new GridLayoutManager(mContext, 7));
        binding.rv.addItemDecoration(new GridItemDecoration(7, ConvertUtils.dp2px(10), true));
        mCylinderBlockAdapter = new CylinderBlockAdapter();
        mCylinderBlockAdapter.setUniversalCallback(this::onUniversalCallback);
        binding.rv.setAdapter(mCylinderBlockAdapter);
        /**
         * 弹框
         */
        mCylinderBlockOperationPopup = new CylinderBlockOperationPopup(mContext);

    }


    @Override
    public void initData() {
        super.initData();
        /**
         * 展示
         */
        mCylinderBlockAdapter.setCylinderBlockEntityList(CylinderBlockEntityDao.getInstance().getAllList());

    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        /**
         *  回调
         */
        mCylinderBlockOperationPopup.setOnGeneralCallback(new OnGeneralCallback() {
            @Override
            public void onGeneralCallback(int GeneralCallback) {
                switch (GeneralCallback) {
                    case 10:
                        //绑定试剂
                        Intent mIntent = new Intent(mContext, ReagentActivity.class);
                        mIntent.putExtra("bound", true);
                        startActivityForResult(mIntent, 1001); // 0是自己定的requestCode

                        break;
                    case 20:
                        //解除绑定


                        break;
                    case 30:
                        //取消染色
                        /**
                         * 染色中
                         */
                        mCylinderBlockEntity.CylinderDyeing = false;
                        //更新
                        mCylinderBlockEntity.update();
                        //
                        initData();

                        break;
                    case 40:
                        //测试染色
                        /**
                         * 染色中
                         */
                        mCylinderBlockEntity.CylinderDyeing = true;
                        //更新
                        mCylinderBlockEntity.update();
                        //
                        initData();

                        break;
                }
            }
        });


    }

    @Override
    public void onUniversalCallback(int position, Object mObject) {
        /**‘
         *
         */
        mCylinderBlockEntity = (CylinderBlockEntity) mObject;
        /**
         *
         */
        new XPopup
                .Builder(mContext)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .autoOpenSoftInput(false)
                .asCustom(mCylinderBlockOperationPopup)
                .show();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode) {
            case 1001:
                if (resultCode == 3002) {
                    if (null != mCylinderBlockEntity) {
                        long mReagentId = data.getLongExtra("ReagentId", 0L);
                        //试剂编号
                        mCylinderBlockEntity.ReagentId = mReagentId;
                        /**
                         * 更新
                         */
                        if (mCylinderBlockEntity.update()) {
                            InitializeUtils.getDefaultMaker().show(mCylinderBlockEntity.CylinderBlockNumber + "号绑定成功");
                            /**
                             * 刷新数据
                             */
                            initData();
                        }
                    }
                }
                break;
        }


        super.onActivityResult(requestCode, resultCode, data);
    }
}
